#ifndef FKCLIENT_H
#define FKCLIENT_H

#include "FKObject.h"
#include "FKObjectsData.h"

#include <QStringList>
#include <QVariant>

class FKUser;
class QTimer;

typedef QList<QMap<int,QVariant> > FKRoomDataList;
Q_DECLARE_METATYPE(FKRoomDataList)

class FKOBJECTSSHARED_EXPORT FK_OBJECT(FKClient,FKObject)
    Q_OBJECT
    Q_PROPERTY(QString cn READ name WRITE setName) //name
    Q_PROPERTY(QString cp READ password WRITE setPassword) //password
    Q_PROPERTY(QStringList cul READ getUserList WRITE setUserList) //change user list
public:
//    FKClient(FKRoom* room=0, qint32 objectID=-1);
    FKClient(QString clientName, QString password);
//    ~FKClient();
    static FKClient* getClient(QString clientName);
    void manageEvent(FKEvent* event);
    void manageAction(FKEvent* action);
    void setName(QString name);
    QString name() const;
    void setPassword(QString password);
    QString password() const;
    QList<FKUser*> users() const;
    QStringList userNames() const;
    void setServerFiles(QVariant lst){_serverFiles=lst;}
    QVariant serverFiles() const{return _serverFiles;}
    void setConnected();
    void setDisconnected();
    bool connected()const{return _connected;}
    void beginReconnect(){_reconnecting=true;}
    void reconnect();
    void roomCheck();
    bool checkRoomAvaliable(const QString& roomClass);
signals:
    void userListChanged(QStringList userList);
    void currentUserListChanged(QStringList userList);
    void systemMessage(QString message);
    void refreshRoomList(QList<QMap<int,QVariant> > list);
    void submitUsers();
private slots:
    void removeUser(QObject *o);
    void releaseClient();
private:
    virtual void pack(QDataStream &stream);
    virtual bool load(QDataStream &stream);
    void rememberUserList();
    QStringList getUserList() const;
    void setUserList(QStringList userList);

    void addUser(FKUser* user);
    void removeUser(FKUser* user);
    QSet<FKObject*> _hasObjects;
    QMap<qint32,FKUser*> _users;
    QString _name;
    QString _password;
    QStringList _userList;
    static QMap<QString,FKClient*> _clients;
    QVariant _serverFiles;
    bool _connected;
    bool _reconnecting;
    QTimer* _deleteTimer;
    static const int _deletingDelay;
    QStringList _avaliableRoomTypes;

    static int identifier;
    static int regMetaTypes();
    QSet<FKObject*> sharedVisors();
    void visionChanged();

    void A_setName(FKEvent* event);
    void A_setPassword(FKEvent* event);
    void AE_gotSystemMessage(FKEvent* message);
    void A_updateRoomList(FKEvent *event);
    void E_updateRoomList(FKEvent* event);
    void A_addUser(FKEvent *event);
    void A_removeUser(FKEvent *event);
    void E_addUser(FKEvent *event);
    void E_removeUser(FKEvent *event);
    void E_createObjects(FKEvent *event);
    void E_deleteObjects(FKEvent *event);
    void A_addUserToList(FKEvent *event);
    void A_removeUserFromList(FKEvent *event);
    void E_submitUsers(FKEvent* event);
    void a_avaliableRoomTypes(FKEvent* event);
    void e_initRoomModule(FKEvent* event);

    FK_NO_SERVANT
    FK_NO_CUSTOM_VALUES
};


#endif // FKCLIENT_H
